package com.supor.aiot.common.base;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

import com.android.baseconfig.common.observer.NetConnectionObserver;
import com.supor.aiot.common.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

import androidx.annotation.LayoutRes;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.AndroidViewModel;

/**
 * @author zook
 */
public class BaseMVVMEnhanceActivity<M extends AndroidViewModel, V extends ViewDataBinding>
        extends BaseMVVMActivity<M, V> implements NetConnectionObserver {

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        mBaseBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_base, null, false);
        setContentView(mBaseBinding.getRoot());

        ParameterizedType superclass = (java.lang.reflect.ParameterizedType) getClass().getGenericSuperclass();
        Class<V> aClass = (Class<V>) superclass.getActualTypeArguments()[1];
        try {
            for (Method method : aClass.getMethods()) {
                System.out.println("Methods_debug: " + method.getName());
            }
            Method method = aClass.getMethod("inflate", LayoutInflater.class,
                    ViewGroup.class, boolean.class);
            bindingView = (V) method.invoke(null, getLayoutInflater(), mBaseBinding.getRoot().findViewById(R.id.container), true);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

        loadingView = ((ViewStub) findViewById(R.id.vs_loading)).inflate();

        // setToolBar(mBaseBinding.toolBar);
        bindingView.getRoot().setVisibility(View.GONE);
        initViewModel();
        setNoTitle();
        showContentView();
    }

    /**
     * 此方法弃用 自动反射获取binding
     *
     * @see BaseMVVMActivity#setContentView(int)
     */
    @Deprecated
    protected int resourceId() {
        return 0;
    }
}